package com.example.waibao.controller;

import com.example.waibao.Component.UserHelper;
import com.example.waibao.entity.ResponseResult;
import com.example.waibao.entity.User;
import com.example.waibao.entity.faq;
import com.example.waibao.entity.notice;
import com.example.waibao.service.NoticeService;
import com.example.waibao.service.impl.SseEmitterServer;
import com.example.waibao.service.impl.SseEmitterServer2;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

@RestController
public class NoticeController {


    @Autowired
    private UserHelper userHelper;

    @Autowired
    private NoticeService noticeService;

    @PostMapping("set_notice")
    private ResponseResult setNotice(@RequestBody notice notice) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        java.util.Date day=new Date();
        String d = format.format(day);
        Date date1 = null;
        try {
            date1 = format.parse(d);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        User user = userHelper.getCurrentUser();
        int school_id = user.getSchool_id();
      //  int id = user.getId().intValue();
        //可以做到有则更新，无则添加
        noticeService.setNotice(notice.getMessage(), user.getId().intValue(), date1, school_id, notice.getType());

        String msg = "全体通知:" + notice.getMessage();
        SseEmitterServer sseEmitterServer = new SseEmitterServer();
        sseEmitterServer.sendAllClientMsg(msg);
        //sseEmitterServer.sendAllClientMsg2(msg,user.getSchool_id());
        //注：可以重复设置公告也就是更新，比如设置公告置顶同样也是用这个接口

        ResponseResult result = new ResponseResult(200, "通告设置成功");
        return result;

    }

    @PostMapping("update_notice")
    private ResponseResult update_notice(@RequestBody notice notice) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        java.util.Date day=new Date();
        String d = format.format(day);
        Date date1 = null;
        try {
            date1 = format.parse(d);
        } catch (ParseException e) {
            e.printStackTrace();
        }


        User user = userHelper.getCurrentUser();
        noticeService.update_notice(notice.getId(),date1, notice.getMessage(), user.getId(), notice.getType());

        String msg = "全体通知:" + notice.getMessage();
        SseEmitterServer sseEmitterServer = new SseEmitterServer();
        sseEmitterServer.sendAllClientMsg(msg);
       // sseEmitterServer.sendAllClientMsg2(msg,user.getSchool_id());
        ResponseResult result = new ResponseResult(200, "公告更新成功");
        return  result;

    }

    @GetMapping("get_notice")
    private ResponseResult get_notice(@RequestParam(name = "page_num", defaultValue = "1")Integer page_num,
                                      @RequestParam(name = "page_size", defaultValue = "5")Integer page_size,
                                      @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate create_time1,
                                      @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate create_time2
                                      ) {
     //   @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate time2 ,@RequestParam(name = "state", defaultValue = "0")String state
     //  List<notice> notices = noticeService.getNotice();
        User user = userHelper.getCurrentUser();
        int school_id = user.getSchool_id();
        PageInfo<notice> dataPageInfo = noticeService.getNotice2(page_num,page_size,create_time1,create_time2,school_id);
      //  ResponseResult result = new ResponseResult(200, "获取通知成功", notices);

        ResponseResult result = new ResponseResult(200, "获取通知成功", dataPageInfo);
        return result;

    }
    @DeleteMapping("delete_notice")
    private ResponseResult delete_notice(@RequestParam("id") int id) {
        System.out.println(id);
        noticeService.delete_notice(id);
        ResponseResult result = new ResponseResult(200, "删除公告成功");
        return result;
    }

    //使用sse实现实时更新，当老师已经解答问题后，告诉前端数据要进行更新操作

//    @RequestMapping(value = "/notice_update", produces = "text/event-stream;charset=UTF-8")
//    public String push() {
//        try {
//            Thread.sleep(1000);
//            //第三方数据源调用
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        return "data:有问题已被解答" + "\n\n";
//    }


    @GetMapping("get_question")
    private ResponseResult get_question(@RequestParam(name = "page_num", defaultValue = "1")Integer page_num,
                                        @RequestParam(name = "page_size", defaultValue = "5")Integer page_size,
                                        @RequestParam(name = "status", defaultValue = "2") int status ) {
        User user = userHelper.getCurrentUser();
        int school_id = user.getSchool_id();
        PageInfo<faq> faqs = noticeService.get_question(page_num,page_size,school_id,status);
     // List<faq> faqs =  noticeService.get_question(school_id,status);
        ResponseResult result = new ResponseResult(200, "获取问题成功", faqs);

        return result;

    }

    @PostMapping("solve_question")
    private ResponseResult solve_question(@RequestBody faq faq) throws IOException {

       // int question_id,String answer,int student_id, int sys_id
        String msg = "你的问题已解决，老师已经回复了你，快去看看吧！";
        //student_id要string字符串类型
        //使用sse实时通知前端问题已被解答
        noticeService.solve_question(faq.getId(),faq.getAnswer(),faq.getSys_id());

        SseEmitterServer sseEmitterServer = new SseEmitterServer();
        sseEmitterServer.sendOneClientMessage(String.valueOf(faq.getUser_id()), msg);

//        SseEmitterServer2.sendMessage(String.valueOf(faq.getUser_id()), msg);
        ResponseResult result = new ResponseResult(200, "解答问题成功");

        return result;

    }

    @DeleteMapping("delete_question")
    public ResponseResult delete_question(@RequestParam("id") int id) {

        noticeService.delete_question(id);

        return  new ResponseResult(200, "删除问题成功");
    }


}
